package com.liuh.edu.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.liuh.edu.common.SuperController;
import com.liuh.edu.pojo.ClassRoom;
import com.liuh.edu.service.IClassRoomService;
import com.liuh.edu.util.MyConstants;

/**
 * 教室控制器类
 * 
 * @author liuh
 *
 */
@Controller
@RequestMapping("/room/")
public class RoomController extends SuperController{

	@Resource
	private IClassRoomService roomService;

	/**
	 * 教室列表信息页面
	 */
	@RequestMapping("/list")
	public String roomInfoList(Model model, @RequestParam(required = false) String crname,
			@RequestParam(value = "currentPageNo", required = false) Integer currentPageNo) {
		currentPageNo = currentPageNo == null ? 1 : currentPageNo;
		Page<ClassRoom> page = getPage(currentPageNo, MyConstants.pageSize);
		model.addAttribute("pageSize", MyConstants.pageSize);
		// 查询分页
		EntityWrapper<ClassRoom> ew = new EntityWrapper<ClassRoom>();
		if (!StringUtils.isEmpty(crname)) {
			ew.like("cr_name", crname);
			model.addAttribute("crname", crname);
		}
		Page<ClassRoom> pageData = roomService.selectPage(page, ew);
		model.addAttribute("pageData", pageData);
		return "classroom/classroom-list";
	}

	/**
	 * 教室新增/修改--页面跳转
	 * 
	 * @param id
	 * @param m
	 * @author 10049
	 */
	@RequestMapping("/toEditRoomView")
	public String toEditClassroomView(@RequestParam(value = "id", required = false) Integer id, Model m) {
		if (id != null && id != 0) {
			ClassRoom classroom = roomService.selectById(id);
			m.addAttribute("classroom", classroom);
		}
		return "classroom/classroom-add";
	}

	/**
	 * 教室新增
	 * 
	 * @param classroom
	 * @param request
	 * @author 10049
	 */
	@RequestMapping("/saveRoom")
	@ResponseBody
	public int doSaveClassroomInfo(ClassRoom classroom, String progector) {
		int result=0;
		int progectorState="是".equals(progector)?1:0;
		classroom.setTouying(progectorState);
		// 根据主键是否为空?新增:修改;
		if(classroom.getCrId()!=null) {
			result=roomService.updateById(classroom)?1:0;
		}else {
			//result=roomService.insert(classroom)?1:0;
			result=roomService.insertRoom(classroom);
		}
		return result;
	}
	
	/**
	 * @desc 新增(事物测试)
	 * @author liuh
	 * @date 2018年7月6日 上午8:48:57
	 */
	@RequestMapping("/saveRoom2")
	public String saveRoom(ClassRoom classroom) {
		roomService.insertRoom(classroom);
		return redirectTo("/room/list");
	}

	/**
	 * 教室删除
	 * 
	 * @param cid
	 * @author 10049
	 */
	@ResponseBody
	@RequestMapping("/delRoom")
	public Map<String,String> deleteClassroomInfo(@RequestParam Integer cid) {
		Map<String, String> result = new HashMap<String, String>();
		if (roomService.deleteById(cid)) {
			result.put("result", "success");
		} else {
			result.put("result", "error");
		}
		return result;
	}
	
	/**
	 * 教室信息Json 显示
	 * 
	 * @param cid
	 * @author 10049
	 */
	@ResponseBody
	@RequestMapping("/showRoom")
	public List<ClassRoom> showClassroomInfo(Integer cid,Model model) {
		//
		System.out.println(model.containsAttribute("cid"));
		System.out.println("cid----"+cid);
		EntityWrapper<ClassRoom> ew = new EntityWrapper<ClassRoom>();
		List<ClassRoom> list=roomService.selectList(ew);
		//return toJson(list);
		return list;
	}
}
